<template>
  <div class="search">
    <el-autocomplete
      v-model="hosname"
      
      :fetch-suggestions="fetchData"
      :trigger-on-focus="false"
      @select="goDetial"
    />
    <!--  :fetch-suggestions="fetchData" 文本发生变化时触发
      :trigger-on-focus="false" 失去焦点时触发
      @select="goDetial" 选择建议时触发，会将选择的建议传入
    -->
    <el-button type="primary" :icon="Search"></el-button>
  </div>
</template>

<script setup lang="ts">
//引入路由器
import { useRouter } from "vue-router";
//引入图标组件
import { Search } from "@element-plus/icons-vue";
//引入接口
import { reqGetHospitalInfo } from "@/api/home";
//引入ts接口类型
import { HospitalInfo } from "@/api/home/type";
//引入组合式api
import { ref } from "vue";
//收集医院搜索关键字
let hosname =ref<string>('')
//创建路由器对象
let $router = useRouter();
const fetchData = async (key:string,ck:any)=>{
  //调用接口
  let reslut:HospitalInfo = await reqGetHospitalInfo(key)
  if(reslut.code === 200){
    //整理数据
    let valueData=reslut.data.map(item=>{
      return {
        value:item.hosname,
        hoscode:item.hoscode,
      }
    })
    ck(valueData)
  }
}
//点击推荐项的回调函数
const goDetial = (item:any)=>{
console.log(item);

  
  //路由跳转
  $router.push({
    path:'/hospital/detial',
    query:{
      hoscode:item.hoscode
    }
  })
}
</script>

<style scoped lang="scss">
.search {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 15px 0;
  width: 100%;
  height: 60px;
//深度选择器
    :deep(.el-input__wrapper) {
        width: 600px;
        margin-right: 10px;
    }
}
</style>
